package com.htdata.iiot.tools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.LongAdder;

import com.htdata.iiot.api.pojo.StreamingData;
import com.htdata.iiot.api.pojo.StreamingMetricData;
import com.htdata.iiot.db.DbConnectionPool;
import com.htdata.iiot.db.JDBCUtils;

public class Auto2Rest {
	private static DbConnectionPool pool = DbConnectionPool.getPool();
	static long num = 0;
	static long threds = 50;
	static long st = System.currentTimeMillis();
	static String url = "http://192.168.0.32:80/iot/data_acquire";
	static LongAdder mAdder = new LongAdder();
	static LongAdder adder = new LongAdder();

	public static void main(String[] args) {
		for (String arg : args) {
			String[] split = arg.split("=");
			switch (split[0]) {
			case "threads":
				threds = Integer.parseInt(split[1]);
				break;
			case "url":
				url = split[1];
				break;

			default:
				break;
			}
		}

		List<StreamingData> sList = new ArrayList<StreamingData>();
		// 1.查租户

		// 1.根据租户ID获取设备
		String sql1 = "SELECT "
				    + "t.id AS iot,"
				    + "t.api_key AS accesskey,"
				    + "d.id AS id,"
				    + "d.serial_number AS equipment "
				    + "FROM tenant t, thing_device d "
				    + "WHERE d.tenant_id=t.id "
				    + "  AND d.deleted=0"
				    + "  AND t.deleted=0"
				    + "  AND d.struct_id!=0";

		Connection conn = pool.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		ResultSet executeQuery = null;
		PreparedStatement prepareStatement = null;
		try {
			ps = conn.prepareStatement(sql1);
			rs = ps.executeQuery();
			while (rs.next()) {
				int iot = rs.getInt("iot");
				String accesskey = rs.getString("accesskey");
				int deviceId = rs.getInt("id");
				String equipment = rs.getString("equipment");

				StreamingData streamingData = new StreamingData();
				streamingData.setIot(iot);
				streamingData.setAccesskey(accesskey);
				streamingData.setEquipment(equipment);
				// 2.根据设备获取测点
				String sql2 = "SELECT "
				            + "t1.id AS id,"
						    + "t2.name AS NAME "
						    + "FROM thing_device_component_rel t1,"
						    + "thing_data_stream t2,"
						    + "thing_component t3 "
						    + "WHERE t1.component_id=t3.id"
						    + "  AND t3.deleted=0"
						    + "  AND t2.component_id=t3.id" 
						    + "  AND t1.deleted=0"
						    + "  AND t1.device_id=?"
						    + "  AND t2.deleted=0"
						    + "  AND t2.tenant_id=1";
				
				prepareStatement = conn.prepareStatement(sql2);
				prepareStatement.setInt(1, deviceId);
				executeQuery = prepareStatement.executeQuery();
				List<StreamingMetricData> mList = new ArrayList<StreamingMetricData>();
				while (executeQuery.next()) {
					String name = executeQuery.getString("name");
					StreamingMetricData metricData = new StreamingMetricData();
					metricData.setK(name);
					mList.add(metricData);
				}
				streamingData.setData(mList);
				sList.add(streamingData);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.close(ps, rs, conn);
			JDBCUtils.close(prepareStatement, executeQuery, null);
		}
		
		System.out.println(sList.size());
		long startTime = System.currentTimeMillis();

		for (int a = 0; a < threds; a++) {
			new Thread(new Post(sList, url)).start();
		}

		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}

					long i = adder.sum();
					long mi = mAdder.sum();

					long endTime = System.currentTimeMillis();
					long useTime = endTime - startTime;
					System.out.println("写入 " + i + " 条数据,累计测点数：" + mi + "，平均速率 " + (i * 1000 / useTime) + "，测点速率 "
							+ (mi * 1000 / useTime));
				}

			}
		}).start();

	}

}
